package com.example.choupaiapp.main.fragment.message;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.fragment.CommonFragment;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.message.ByIdDetailActivity;
import com.example.choupaiapp.main.activity.message.ContentNoPassActivity;
import com.example.choupaiapp.main.activity.mine.MineEnergyActivity;
import com.example.choupaiapp.main.activity.mine.MineWalletActivity;
import com.example.choupaiapp.main.bean.NoticeBean;
import com.example.choupaiapp.main.constant.Url;
import com.example.choupaiapp.main.view.ScaleImageView;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2019/10/30.
 */

public class NoticeFragment extends CommonFragment implements OnRefreshLoadMoreListener {

    @BindView(R.id.listview)
    ListView listview;
    Unbinder unbinder;
    private QuickAdapter<NoticeBean.PageBean.ListBean> quickAdapter;
    private int pageNum = 1;
    private int pageSize = 10;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_img)
    ImageView empty_img;

    public static NoticeFragment newInstance() {
        return new NoticeFragment();
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notice, container, false);
        unbinder = ButterKnife.bind(this, view);

        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));

        quickAdapter = new QuickAdapter<NoticeBean.PageBean.ListBean>(context, R.layout.item_notice) {
            @Override
            protected void convert(BaseAdapterHelper helper, NoticeBean.PageBean.ListBean item) {
                ScaleImageView photo = helper.getView(R.id.photo);
                if (!TextUtils.isEmpty(item.getImage())) {
                    photo.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(item.getImage())
                            .dontAnimate()
                            .placeholder(R.color.color_f3f3)
                            .centerCrop()
                            .into(photo);
                } else {
                    photo.setVisibility(View.GONE);
                }

                helper.setText(R.id.title, item.getTitle());
                helper.setText(R.id.description, item.getContent());

                helper.setText(R.id.time, item.getCreateTime());
            }
        };

        listview.setAdapter(quickAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                if ((quickAdapter.getItem(i).getOperation() + "").startsWith("3")) {
                    startActivity(MineEnergyActivity.class);
                } else {
                    if (quickAdapter.getItem(i).getOperation() == 2004) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2005) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2006) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2007) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2008) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2009) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2001) {
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("id", quickAdapter.getItem(i).getContentId() + "");
                        startActivity(ByIdDetailActivity.class, bundle1);
                    } else if (quickAdapter.getItem(i).getOperation() == 2002) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", quickAdapter.getItem(i).getTitle());
                        bundle.putString("desc", quickAdapter.getItem(i).getContent());
                        bundle.putString("time", quickAdapter.getItem(i).getCreateTime());
                        startActivity(ContentNoPassActivity.class, bundle);
                    } else if (quickAdapter.getItem(i).getOperation() == 2003) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", quickAdapter.getItem(i).getTitle());
                        bundle.putString("desc", quickAdapter.getItem(i).getContent());
                        bundle.putString("time", quickAdapter.getItem(i).getCreateTime());
                        startActivity(ContentNoPassActivity.class, bundle);
                    } else if (quickAdapter.getItem(i).getOperation() == 2010) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2011) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2012) {
                        startActivity(MineEnergyActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2013) {
                        startActivity(MineEnergyActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2014) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2015) {
                        startActivity(MineWalletActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2016) {
                        startActivity(MineEnergyActivity.class);
                    } else if (quickAdapter.getItem(i).getOperation() == 2017) {
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("id", quickAdapter.getItem(i).getContentId() + "");
                        startActivity(ByIdDetailActivity.class, bundle1);
                    }
                }
            }
        });

        return view;
    }

    @Override
    protected void initData() {
        showDialog(context, null);
        HttpParams params = new HttpParams();
        params.put("limit", pageSize);
        params.put("page", pageNum++);
        finalOkGo.requestHead(RequestType.POSTHEAD, Url.MESSAGE_NOTICE, params, Url.MESSAGE_NOTICE, new Callback<NoticeBean>() {
            @Override
            public void onSuccess(NoticeBean noticeBean) {
                dismissDialog();
                if (noticeBean.getCode() == Contacts.SUCCESS) {
                    EventBus.getDefault().post("clearMessageRemind");
                    if (pageNum - 1 == 1) {
                        if (noticeBean.getPage().getList() == null || noticeBean.getPage().getList().size() == 0) {
                            empty_img.setVisibility(View.VISIBLE);
                            empty_img.setImageResource(R.mipmap.icon_no_notify);
                        } else {
                            empty_img.setVisibility(View.GONE);
                        }
                        quickAdapter.clear();
                        quickAdapter.addAll(noticeBean.getPage().getList());
                    } else {
                        quickAdapter.addAll(noticeBean.getPage().getList());
                    }

                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    quickAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (noticeBean.getPage().getTotalCount() % pageSize == 0) {
                        pagecount = noticeBean.getPage().getTotalCount() / pageSize;
                    } else {
                        pagecount = noticeBean.getPage().getTotalCount() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        refreshLayout.setNoMoreData(true);
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty_img.setVisibility(View.VISIBLE);
                        empty_img.setImageResource(R.mipmap.icon_no_net);
                    }
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    if (noticeBean.getCode() == Contacts.TOKEN_EMPTY) {
                    } else {
                        showImageToas(noticeBean.getMsg());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                if (pageNum - 1 == 1) {
                    empty_img.setVisibility(View.VISIBLE);
                    empty_img.setImageResource(R.mipmap.icon_no_net);
                }
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        initData();
    }
}
